﻿using ZYCommon;
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;

namespace TextEditor
{


    public class ZYTextTableCell : ZYTextTableBase
    {
        protected int colIndex = 0;
        private int holdRowsCount = 1;
        private bool isLoadFromFile = true;
        private int minHeight = 18;
        private int minWidth = 18;
        private ZYTextTableRow ownerRow = null;
        private ZYTextTable ownerTable = null;
        private const int reserveVMargin = 1;
        private bool selected = false;

        public ZYTextTableCell()
        {
            base.ID = StringCommon.AllocObjectName();
        }

        public override bool FromXML(XmlElement myElement)
        {
            if (base.FromXML(myElement))
            {
                if (this.VMContinue)
                {
                    base.ChildElements.Clear();
                }
                return true;
            }
            return false;
        }

        public ZYTextParagraph GetFirstParagraph()
        {
            foreach (ZYTextElement element in base.myChildElements)
            {
                if (element is ZYTextParagraph)
                {
                    return (ZYTextParagraph)element;
                }
            }
            return null;
        }

        public ZYTextParagraph GetNextParagraph(ZYTextElement startElement)
        {
            if (base.myChildElements.Count != 0)
            {
                for (int i = base.myChildElements.IndexOf(startElement); i < base.myChildElements.Count; i++)
                {
                    if (base.myChildElements[i] is ZYTextParagraph)
                    {
                        return (ZYTextParagraph)base.myChildElements[i];
                    }
                }
            }
            return null;
        }

        public override string GetXMLName()
        {
            return "cell";
        }

        public override bool HandleClick(int x, int y, MouseButtons Button)
        {
            return base.HandleClick(x, y, Button);
        }

        public override bool HandleDblClick(int x, int y, MouseButtons Button)
        {
            return base.HandleDblClick(x, y, Button);
        }

        public override bool HandleMouseDown(int x, int y, MouseButtons Button)
        {
            return base.HandleMouseDown(x, y, Button);
        }

        public override bool HandleMouseMove(int x, int y, MouseButtons Button)
        {
            return base.HandleMouseMove(x, y, Button);
        }

        public override bool HandleMouseUp(int x, int y, MouseButtons Button)
        {
            return base.HandleMouseUp(x, y, Button);
        }

        public override bool isNewLine()
        {
            return false;
        }

        public override bool RefreshSize()
        {
            if (this.VMContinue)
            {
                base.intHeight = 0;
            }
            else
            {
                base.intHeight = 0;
                foreach (ZYTextElement element in base.myChildElements)
                {
                    element.RefreshSize();
                }
                this.RefreshLineFast(0);
                if (base.intHeight < this.minHeight)
                {
                    base.intHeight = this.minHeight;
                }
            }
            if (base.Attributes.Contains("width"))
            {
                this.Width = base.Attributes.GetInt32("width");
            }
            else if (((this.ownerRow != null) && (this.ownerRow.CellCount != 0)) && (this.ownerRow.Width != 0))
            {
                this.Width = (this.ownerRow.Width - 1) / this.ownerRow.CellCount;
            }
            return true;
        }

        public void RefreshCellHeight()
        {
            if (this.VMContinue)
            {
                base.intHeight = 0;
            }
            else
            {
                base.intHeight = 0;
                if (base.intHeight < this.minHeight)
                {
                    base.intHeight = this.minHeight;
                }
            }
        }

        Pen drawPen = null;
        public override bool RefreshView()
        {
            if (drawPen == null)
                drawPen = this.OwnerDocument.View._CreatePen(Color.Black);
            if ((this.OwnerDocument.OwnerControl != null) && this.OwnerRow.OwnerTable.TableLock == false)
            {
                if (this.Selected && this.OwnerTable.SelectedCells.Count > 1)
                {
                    base.myOwnerDocument.View.FillRectangle(Color.Gray, this.Bounds);
                }
            }
            //if (!this.border)
            //{
            //    base.myOwnerDocument.View.DrawRectangle(drawPen, this.Bounds);
            //}
            //else
            //{
            if (!this.VMContinue)
            {
                string style = this.Style;
                if (!string.IsNullOrEmpty(style))
                {
                    string[] styles = style.Split(';');
                    foreach (string str in styles)
                    {
                        string[] str2 = str.Split(':');
                        if (str2.Length > 1)
                        {
                            if (string.IsNullOrEmpty(str2[1]))
                                continue;
                            if (str2[0] == "border")
                            {
                                foreach (char item in str2[1])
                                {
                                    switch (item)
                                    {
                                        case 'l':
                                            base.myOwnerDocument.View.DrawLine(drawPen, this.Bounds.X, this.Bounds.Y, this.Bounds.X, this.Bounds.Bottom);
                                            break;
                                        case 'r':
                                            base.myOwnerDocument.View.DrawLine(drawPen, this.Bounds.Right, this.Bounds.Y, this.Bounds.Right, this.Bounds.Bottom);
                                            break;
                                        case 't':
                                            base.myOwnerDocument.View.DrawLine(drawPen, this.Bounds.X, this.Bounds.Y, this.Bounds.Right, this.Bounds.Y);
                                            break;
                                        case 'b':
                                            base.myOwnerDocument.View.DrawLine(drawPen, this.Bounds.X, this.Bounds.Bottom, this.Bounds.Right, this.Bounds.Bottom);
                                            break;
                                    }
                                }
                            }

                            if (str2[0] == "borderSlash")
                            {
                                foreach (char ch2 in str2[1])
                                {
                                    if (ch2 == 'n')
                                        base.myOwnerDocument.View.DrawLine(drawPen, this.Bounds.X, this.Bounds.Y, this.Bounds.Right, this.Bounds.Bottom);

                                    if (ch2 == 'm')
                                        base.myOwnerDocument.View.DrawLine(drawPen, this.Bounds.Right, this.Bounds.Y, this.Bounds.X, this.Bounds.Bottom);

                                }
                            }

                            if (str2[0] == "linear")
                            {
                                ;
                            }

                            //if (str2[0] == "borderColor")
                            //{
                            //    this.colorDialog1.Color = ZYCommon.StringCommon.ColorFromHtml(str2[1], Color.Black);
                            //    this.label7.BackColor = this.colorDialog1.Color;
                            //}
                        }
                        //}
                    }
                }
            }
            return base.RefreshView();
        }

        public override Rectangle Bounds
        {
            get
            {
                return base.Bounds;
            }
            set
            {
                base.Bounds = value;
            }
        }

        public override bool ToXML(XmlElement myElement)
        {
            return base.ToXML(myElement);
        }
        public override int Width
        {
            get
            {
                return base.Attributes.GetInt32("width");
            }
            set
            {
                if (value >= this.minWidth)
                    base.Attributes.SetValue("width", value);
            }
        }
        public override TableCellAlign Align
        {
            get
            {
                if (base.Attributes.Contains("align"))
                {
                    base.align = (TableCellAlign)base.Attributes.GetInt32("align");
                }
                else
                {
                    base.Attributes.SetValue("align", (int)base.align);

                }
                return base.align;
            }
            set
            {
                base.align = value;
                base.Attributes.SetValue("align", (int)base.align);
            }
        }

        public int ColIndex
        {
            get
            {
                return this.OwnerRow.Cells.IndexOf(this);
            }
        }

        public override int Top
        {
            get
            {
                return 0;
            }
            set
            {
                base.Top = 0;
            }
        }

        public string Style
        {
            set
            {
                base.Attributes.SetValue("cell-style", value);
            }
            get
            {
                return base.Attributes.GetString("cell-style");
            }
        }

        public override int Height
        {
            get
            {
                if (this.VMContinue)
                {
                    return 0;
                }
                base.intHeight = ((this.RealHeight < base.intHeight) ? base.intHeight : this.RealHeight);
                base.intHeight = ((base.intHeight < this.minWidth) ? this.minWidth : base.intHeight);
                return base.intHeight;
            }
            set
            {
                if (!this.VMContinue)
                {
                    if (value < this.minWidth)
                    {
                        base.intHeight = this.minWidth;
                    }
                    else
                    {
                        base.intHeight = value;
                    }
                }
            }
        }

        public int HoldRowsCount
        {
            get
            {
                return this.holdRowsCount;
            }
            set
            {
                this.holdRowsCount = value;
            }
        }

        public bool IsLoadFromFile
        {
            get
            {
                return this.isLoadFromFile;
            }
            set
            {
                this.isLoadFromFile = value;
            }
        }

        public int colspan
        {
            get { return 0; }
            set { }
        }

        public int rowspan
        {
            get { return 0; }
            set { }
        }

        public Color LineColor
        {
            get
            {
                return base.myAttributes.GetColor("linecolor");
            }
            set
            {
                base.myAttributes.SetValue("linecolor", value);
            }
        }

        public int LineWidth
        {
            get
            {
                base.lineWidth = base.myAttributes.GetInt32("linewidth");
                if (base.lineWidth < 0)
                {
                    base.lineWidth = 1;
                    base.myAttributes.SetValue("linewidth", base.lineWidth);
                }
                return base.lineWidth;
            }
            set
            {
                if ((value >= 1) && (value <= 10))
                {
                    base.lineWidth = value;
                    base.myAttributes.SetValue("linewidth", base.lineWidth);
                }
            }
        }

        public int MinHeight
        {
            get
            {
                return this.minHeight;
            }
            set
            {
                this.minHeight = value;
            }
        }

        public int MinWidth
        {
            get
            {
                return this.minWidth;
            }
            set
            {
                this.minWidth = value;
            }
        }

        public ZYTextTableRow OwnerRow
        {
            get
            {
                return this.ownerRow;
            }
            set
            {
                this.ownerRow = value;
            }
        }

        public ZYTextTable OwnerTable
        {
            get
            {
                return this.ownerTable;
            }
            set
            {
                this.ownerTable = value;
            }
        }

        public int RealHeight
        {
            get
            {
                int minWidth = 0;
                foreach (ZYTextLine line in base.Lines)
                {
                    minWidth += line.FullHeight;
                }
                if (base.Lines.Count < 1)
                {
                    minWidth += this.minHeight + base.OwnerDocument.Info.LineSpacing;
                }
                if (minWidth < this.MinWidth)
                {
                    minWidth = this.MinWidth;
                }
                return minWidth;
            }
        }

        public bool Selected
        {
            get
            {
                return this.selected;
            }
            set
            {
                this.selected = value;
            }
        }

        public bool VMContinue
        {
            get
            {
                return (base.Attributes.GetString("vmerge") == "continue");
            }
            set
            {
                if (value)
                {
                    base.Attributes.SetValue("vmerge", "continue");
                    base.Visible = false;
                }
                else
                {
                    base.Attributes.RemoveAttribute("vmerge");
                    base.Visible = true;
                }
            }
        }

        public bool border
        {
            get
            {
                return (base.Attributes.GetString("isVisble") == "0" ? true : false);
            }
            set
            {
                base.Attributes.SetValue("isVisble", value ? "0" : "1");
            }
        }

        public bool VMStart
        {
            get
            {
                return (base.Attributes.GetString("vmerge") == "restart");
            }
            set
            {
                if (value)
                {
                    base.Attributes.SetValue("vmerge", "restart");
                }
                else
                {
                    base.Attributes.RemoveAttribute("vmerge");
                }
            }
        }

        public bool VisibleBorderLeft
        {
            get
            {
                return false;
            }
        }
        public bool VisibleBorderRight
        {
            get
            {
                return false;
            }
        }
        public bool VisibleBorderTop
        {
            get
            {
                return false;
            }
        }
        public bool VisibleBorderBottom
        {
            get
            {
                return false;
            }
        }
    }
}

